package com.bawei.module_car_space.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_car_space.R
import com.bawei.module_car_space.model.SpaceModelItem
import com.bawei.module_car_space.model.TYPE_EMPTY
import com.bawei.module_car_space.model.TYPE_MAINTAIN
import com.bawei.module_car_space.model.TYPE_OCCUPIED

class SpaceAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {


    private val diffCallback = object : DiffUtil.ItemCallback<SpaceModelItem>() {
        override fun areItemsTheSame(oldItem: SpaceModelItem, newItem: SpaceModelItem): Boolean {
            return oldItem.id == newItem.id // 根据唯一标识判断 item 是否相同
        }

        override fun areContentsTheSame(oldItem: SpaceModelItem, newItem: SpaceModelItem): Boolean {
            return oldItem == newItem // 判断 item 内容是否相同，数据类重写了 equals 可直接用
        }
    }

    fun submitList(newData : List<SpaceModelItem>) = differ.submitList(newData)

    private val differ = AsyncListDiffer(this,diffCallback)
    override fun getItemViewType(position: Int): Int {
        val item = differ.currentList[position]
        return when(item.spaceStatus) {
            "空闲" -> TYPE_EMPTY
            "已占用" -> TYPE_OCCUPIED
            "维护中" -> TYPE_MAINTAIN
            else -> TYPE_EMPTY
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_EMPTY -> EmptyViewHolder(
                inflater.inflate(R.layout.item_car_space_free, parent, false)
            )
            TYPE_OCCUPIED -> OccupiedViewHolder(
                inflater.inflate(R.layout.item_car_space_occupy, parent, false)
            )
            TYPE_MAINTAIN -> MaintainViewHolder(
                inflater.inflate(R.layout.item_car_space_maintain, parent, false)
            )
            else -> EmptyViewHolder(
                inflater.inflate(R.layout.item_car_space_free, parent, false)
            )
        }
    }

    override fun getItemCount(): Int {
        return differ.currentList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = differ.currentList[position]
        when (holder) {
            is EmptyViewHolder -> holder.bind(item)
            is OccupiedViewHolder -> holder.bind(item)
            is MaintainViewHolder -> holder.bind(item)
        }
    }

    // 空闲状态 ViewHolder
    class EmptyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvSpaceNumber = itemView.findViewById<TextView>(R.id.space_text)
        private val tvArea = itemView.findViewById<TextView>(R.id.areaId_text)
        private val tvType = itemView.findViewById<TextView>(R.id.spaceType_text)
        private val tvStatus = itemView.findViewById<TextView>(R.id.message_status)

        fun bind(item: SpaceModelItem) {
            tvSpaceNumber.text = item.spaceNumber
            tvArea.text = "所属区域：${item.areaName}"
            tvType.text = "车位类型：${item.spaceType}"
            tvStatus.text = item.spaceStatus
        }
    }

    // 已占用状态 ViewHolder
    class OccupiedViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvSpaceNumber = itemView.findViewById<TextView>(R.id.space_text)
        private val tvArea = itemView.findViewById<TextView>(R.id.areaId_text)
        private val tvType = itemView.findViewById<TextView>(R.id.spaceType_text)
        private val tvUser = itemView.findViewById<TextView>(R.id.username_text)
        private val tvLicense = itemView.findViewById<TextView>(R.id.licensePlate_text)
        private val tvStatus = itemView.findViewById<TextView>(R.id.message_status)

        fun bind(item: SpaceModelItem) {
            tvSpaceNumber.text = item.spaceNumber
            tvArea.text = "所属区域：${item.areaName}"
            tvType.text = "车位类型：${item.spaceType}"
            tvUser.text = "使用人：${item.userName}"
            tvLicense.text = "车牌号：${item.licensePlate}"
            tvStatus.text = item.spaceStatus
        }
    }

    // 维护中状态 ViewHolder
    class MaintainViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvSpaceNumber = itemView.findViewById<TextView>(R.id.space_text)
        private val tvArea = itemView.findViewById<TextView>(R.id.areaId_text)
        private val tvType = itemView.findViewById<TextView>(R.id.spaceType_text)
        private val tvRemark = itemView.findViewById<TextView>(R.id.remarks_text)
        private val tvStatus = itemView.findViewById<TextView>(R.id.message_status)

        fun bind(item: SpaceModelItem) {
            tvSpaceNumber.text = item.spaceNumber
            tvArea.text = "所属区域：${item.areaName}"
            tvType.text = "车位类型：${item.spaceType}"
            tvRemark.text = "维护原因：${item.remarks}"
            tvStatus.text = item.spaceStatus
        }
    }

}